package com.example.allplan.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.allplan.Fragment.ChouQianFragment;
import com.example.allplan.Fragment.JiHuaFragment;
import com.example.allplan.Fragment.NaoZhongFragment;
import com.example.allplan.Fragment.SheZhiFragment;
import com.example.allplan.MyApplication;
import com.example.allplan.R;

import java.util.ArrayList;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    RadioGroup daohanglan_radiogroup;
    Fragment mzhanding_fragment;
    FragmentTransaction transaction;
    ArrayList<Fragment> mFragements;
    int containerId = R.id.shou_ye_fragment_rong_qi;
    boolean isExit = false, shifoushuangji;
    Handler mHandler;
    MyApplication myapp;

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    void init() {
        myapp = MyApplication.getInstance();
        mHandler = new Handler() {


            public void handleMessage(Message msg) {

                super.handleMessage(msg);
                isExit = false;
            }

        };
        daohanglan_radiogroup = findViewById(R.id.dao_hang_lan_group);
        daohanglan_radiogroup.setOnCheckedChangeListener(this);
        FragmentManager mFragmentManager = getSupportFragmentManager();
        //用于页面切换的framelayout容器

        mFragements = new ArrayList<Fragment>(4);
        mFragements.add(new JiHuaFragment());
        mFragements.add(new NaoZhongFragment());
        mFragements.add(new ChouQianFragment());
        mFragements.add(new SheZhiFragment());
        addFragment(0);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!myapp.shifoushuangji) {
                tuichuchengxu();
            } else {
                exit();
            }

            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            tuichuchengxu();

        }

    }

    private void tuichuchengxu() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
        System.exit(0);
    }

    protected void addFragment(int n) {
        if (mFragements.get(n) != null) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (mFragements.get(n).isAdded()) {
                if (mzhanding_fragment != null) {
                    transaction.hide(mzhanding_fragment).show(mFragements.get(n));
                } else {
                    transaction.show(mFragements.get(n));
                }
            } else {
                if (mzhanding_fragment != null) {
                    transaction.hide(mzhanding_fragment).add(containerId, mFragements.get(n));
                    transaction.hide(mFragements.get(n)).show(mFragements.get(n));
                } else {
                    transaction.add(containerId, mFragements.get(n));
                    transaction.hide(mFragements.get(n)).show(mFragements.get(n));
                }
            }
            mzhanding_fragment = mFragements.get(n);
            transaction.commit();
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.ji_hua_dao_hang_niu) {
            addFragment(0);
        } else if (checkedId == R.id.nao_zhong_dao_hang_niu) {
            addFragment(1);
        } else if (checkedId == R.id.chou_qian_dao_hang_niu) {
            addFragment(2);
        } else if (checkedId == R.id.she_zhi_dao_hang_niu) {
            addFragment(3);
        }
    }
}
